<template>
    <div class="unit-index yuan-container">
        <a-card title="产品大类装量管理">
            <div class="filter-panel" style="overflow-hidden">
                <div class="filter-list" style="float: right; margin: 0 0 10px 0;">
                    <a-button style="margin-right: 0px;" type="primary" @click="add" v-if="$utilCheckPermission(['productKindDiscretionAdd'])">新增</a-button>
                </div>
            </div>
            <a-spin tip="Loading..." :spinning="loading">
                <a-table bordered :columns="columns" :data-source="pageList" :pagination="pagination" :row-key="getRowKey"></a-table>
            </a-spin>
        </a-card>
        <a-modal width="600px" :visible="visible" :title="modalOption.title" @ok="modalOption.onOk" @cancel="modalOption.onCancel"
                 :okText="modalOption.okText" :cancelText="modalOption.cancelText" class="yuan-modal unit-add-modal">
            <a-spin tip="Loading..." :spinning="formLoading">
                <a-form-model layout="inline" class="yuan-form" ref="form" :model="formData" :rules="rules"
                              :label-col="{ style: { width: '100px' } }" :wrapper-col="{ style: { width: 'calc(100% - 120px)' } }">
                    <div class="form-panel">
                        <div class="form-panel-box yuan-form-col3">
                            <a-form-model-item class="yuan-form-item" label="产品类型" prop="productType">
                                <a-select class="yuan-form-input" v-model="formData.productType" placeholder="请选择产品类型">
                                    <a-select-option value="中药饮片">中药饮片</a-select-option>
                                    <a-select-option value="直服饮片">直服饮片</a-select-option>
                                    <a-select-option value="单方饮片">单方饮片</a-select-option>
                                </a-select>
                            </a-form-model-item>
                            <a-form-model-item v-if="formData.productType == '中药饮片'" class="yuan-form-item" label="中药等级" prop="medicineClass">
                                <a-radio-group class="yuan-form-input" v-model="formData.medicineClass" placeholder="请选择中药等级">
                                    <a-radio value="统货">统货</a-radio>
                                    <a-radio value="选货">选货</a-radio>
                                </a-radio-group>
                            </a-form-model-item>
                            <a-form-model-item class="yuan-form-item" label="产品大类" prop="productBigKind">
                                <a-input class="yuan-form-input" v-model="formData.productBigKind" placeholder="请输入产品大类"></a-input>
                            </a-form-model-item>
                            <a-form-model-item class="yuan-form-item" label="装量" prop="discretion">
                                <a-input class="yuan-form-input" v-model="formData.discretion" placeholder="请输入产品大类装量"></a-input>
                            </a-form-model-item>
                        </div>
                    </div>
                </a-form-model>
            </a-spin>
        </a-modal>
    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { h } from "vue";
import nextApi from "@/api/nextApi";
@Component({
    name: "productVolumeIndex",
    components: {},
})
export default class extends Vue {
    loading = false;
    pagination = {
        pageSize: 10,
        current: 1,
        total: 0,
        showTotal: (total: number) => `共${total}条`,
        onChange: this.pageChange,
    };
    pageList: { id: string; productBigKind: string; discretion:string }[] = [];
    columns = [
        {
            title: "序号",
            dataIndex: "index",
            key: "index",
            align: "center",
            customRender: (text: string, record: any, index: number) => `${index + 1}`,
        },
        {
            align: "center",
            dataIndex: "productBigKind",
            key: "productBigKind",
            title: "产品大类",
        },
        {
            align: "center",
            dataIndex: "medicineClass",
            key: "medicineClass",
            title: "中药等级",
            customRender: (text: string, record: any, index: number) => {
                if (record.medicineClass){
                    return record.medicineClass;
                }else{
                    return '-';
                }
            },
        },
        {
            align: "center",
            dataIndex: "discretion",
            key: "discretion",
            title: "产品大类装量",
        },
        {
            align: "center",
            dataIndex: "productType",
            key: "productType",
            title: "产品类型",
        },
        {
            align: "center",
            dataIndex: "action",
            key: "action",
            title: "操作",
            width: 260,
            customRender: (text: string, record: any, index: number) => {
                const editBtn = h(
                    "a-button",
                    {
                        style: { marginRight: "5px" },
                        props: {
                            icon: "edit",
                            type: "primary",
                            size: "small",
                        },
                        on: { click: () => this.editDetail(record) },
                    },
                    "编辑"
                )
                const deleteBtn = h(
                    "a-popconfirm",
                    {
                        props: {
                            title: "确定要删除吗?",
                            okText: "确定",
                            cancelText: "取消"
                        },
                        on: {
                            confirm: () => this.clickDelete(record.id)
                        }
                    },
                    [
                        h(
                            "a-button",
                            {
                                style: { marginRight: "5px" },
                                props: {
                                    icon: "delete",
                                    type: "danger",
                                    size: "small"
                                }
                            },
                            "删除"
                        )
                    ]
                )
                const btn: any[] = [];
                if (this.$utilCheckPermission(['productKindDiscretionCompile'])) btn.push(editBtn);
                if (this.$utilCheckPermission(['productKindDiscretionRemove'])) btn.push(deleteBtn);
                return h("div", btn);
            },
        },
    ];
    formLoading = false;
    modalOption = {
        title: "新增大类/装量",
        okText: "保存",
        cancelText: "取消",
        onOk: () => {
            this.modalOk();
        },
        onCancel: () => {
            this.closeModal();
        },
    };
    formData: any = {};
    rules = {
        productType:[{ required: true, message: "请输入产品类型", trigger: "blur"}],
        medicineClass:[{  required: true, message: "请选择中药等级", trigger: "blur"}],
        productBigKind: [{ required: true, message: "请输入大类名称", trigger: "blur" }],
        discretion:[{ required: true, message: "请输入大类装量", trigger: "blur" }]
    };
    getRowKey(item: any) {
        return item.id;
    }

    async getList() {
        this.pageList = [];
        try {
            this.loading = true;
            const res = await nextApi.getProductVolumeList({});
            this.pageList = res.data;
        } finally {
            this.loading = false;
        }
    }
    resetList() {
        this.getList();
    }

    visible = false;
    editDetail(item: { id: string,productBigKind:string,discretion:string,productType:string,medicineClass:string }) {
        this.formData = {
            id: item.id,
            productBigKind: item.productBigKind,
            discretion: item.discretion,
            productType:item.productType,
            medicineClass:item.medicineClass
        };
        this.visible = true;
    }
    add() {
        this.formData = {}
        this.visible = true;
    }
    pageChange(page: number, pageSize: number) {
        this.pagination.pageSize = pageSize;
        this.pagination.current = page;
        this.getList();
    }
    async mounted() {
        this.getList();
    }
    modalOk() {
        (this.$refs.form as any).validate(async (valid: any) => {
            if (valid) {
                this.formLoading = true;
                try {
                    const res = await nextApi.addProductVolume(this.formData)
                    this.$message.success(res.msg);
                    this.formData = {};
                    (this.$refs.form as any).resetFields();
                    this.resetList();
                    this.visible = false;
                } finally {
                    this.formLoading = false;
                }
            }
        });
    }
    closeModal() {
        this.visible = false;
    }
    async clickDelete(id: string) {
        this.loading = true;
        try {
            const res = await nextApi.delProductVolume(id);
            this.$message.success(res.msg);
            this.resetList();
        } finally {
            this.loading = false;
        }
    }
}
</script>
<style lang="scss">
.unit-index {
    padding: 20px;
    .botton-box {
        text-align: center;
    }
}
</style>
